{extend name="public:base" /}

{block name="style"}
    <style type="text/css">
        .menu-list{padding: 10px;}
        .menu-list li{line-height: 18px;height: 18px;padding: 5px 0px;}
        .menu-list li input{display: block;float: left; height: 18px;width: 18px;cursor: pointer;}
        .menu-list li span{display: block;float: left; line-height: 18px; padding-left: 7px; font-size: 15px;color: #5e5e5e;}
    </style>
{/block}
{block name="content"}
<div style="height: 38px;">
    <div style="float: left;">
        <input type="hidden" id="oldPrivilege" value='{$oldPrivilege}'>
        <button class="layui-btn layui-btn-normal" onclick="updataPrivilege({$role_id})">更新权限</button>
    </div>
    <div style="float: left; padding-left: 30px;">
        <form class="layui-form" action='{:url("Privilege/index")}'>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">模块选择</label>
                    <div class="layui-input-inline">
                        <select name="role_id" lay-verify="">
                            {foreach $roleList as $v}
                                <option value="{$v.id}" <?php if( $v['id']== $role_id ){ echo "selected"; } ?> >{$v["name"]}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="layui-inline">

                    <button class="layui-btn" lay-submit lay-filter="formDemo">确定</button>

                </div>
            </div>

        </form>
    </div>
    <div class="clear"></div>
</div>

<div class="layui-collapse" style="margin-top: 30px;">
    {foreach $list as $v}

    <div class="layui-colla-item">
        <h2 class="layui-colla-title">{$v.name}</h2>
        <div class="layui-colla-content layui-show">
            <button class="layui-btn layui-btn-small" onclick="selectAllFuc(this)">全选</button>
            <ul class="menu-list">
                {foreach $v.list as $val}
                    <li>
                        <input type="checkbox" value="{$val.role_id}" {$val['status'] == 1 ? 'checked' : ''} >
                        <span>{$val.name}</span>
                        <div class="clear"></div>
                    </li>
                {/foreach}
            </ul>
        </div>
    </div>

    {/foreach}

</div>

{/block}

{block name="javascript"}
<script src="__ADMIN__/js/fromCommon.js" charset="utf-8"></script>

<script type="text/javascript">
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form;

    });
    layui.use('element', function(){
        var element = layui.element;

        //….
    });


    function selectAllFuc(id){
        $(id).next().find("input").each(function(index){

            if(!$(this).is(':checked')){

                $(this).prop("checked","true");
            }
        });
    }

    function updataPrivilege(role_id){
        var  oldPrivilege = $("#oldPrivilege").val();

        var json_str = '';
        $(".layui-collapse input").each(function(index){
            if( $(this).is(':checked') ){
                var  role_id = $(this).val();
                json_str += role_id+',';
            }

        });
        json_str = json_str.substring(0,json_str.length-1);
        $.ajax({
            type: 'POST',
            url: '{:url("Privilege/edit")}',
            dataType: 'json',
            data: {role_id:role_id,oldPrivilege:oldPrivilege,newPrivilege:json_str},
            success: function (da) {
                layui.use("layer",function(){
                    var layer = layui.layer;
                    layer.open({
                        content: "操作成功"//
                        ,yes: function(index, layero){
                            location.reload();
                            layer.close(index);
                        }
                        ,time:3000
                        ,end: function () {
                            location.reload();
                        }
                    });

                });
            }
        });

    }

</script>
{/block}